//
//  CXClassFicaAllThemeViewController.m
//  GiftThat
//
//  Created by ChuXiang on 16/1/8.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "CXClassFicaAllThemeViewController.h"
#import "AllThemeCollectionViewCell.h"


@interface CXClassFicaAllThemeViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *colletion;
@property (nonatomic, strong) NSMutableArray *dataSource;
//-------刷新
//tableview方向判断
@property (nonatomic, assign) CGFloat contenty;


@end
//-------刷新
//限制一次刷新
static int scrolltimes = 0;
//现在数据个数
static int numOfAll = 0;
//-------
@implementation CXClassFicaAllThemeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.title = @"全部专题";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonDidPress:)];
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:16],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    self.dataSource = [NSMutableArray arrayWithCapacity:0];

    [self getData:kLookAllGETURL withOffset:@"0"];
    [self createCollection];
    
}


#pragma mark -GetDataSource 
- (void)getData:(NSString *)url  withOffset:(NSString *)offset {

    
    NSString *getUrl = [NSString stringWithFormat:@"%@%@", url, offset];

    [CXNetWorkTool getWithUrl:getUrl parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            NSDictionary *bannerDic = result[@"data"];
            NSArray *AllArray = bannerDic[@"collections"];
            for (NSDictionary *dic in AllArray) {
                CXClassProModel *model = [[CXClassProModel alloc] initWithDictionary:dic];
                [self.dataSource addObject:model];
            }
            scrolltimes = 0;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.colletion reloadData];
            });

        }
        

    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

#pragma mark - CreateCollection
- (void)createCollection {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    self.colletion = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 113) collectionViewLayout:flow];
    self.colletion.delegate = self;
    self.colletion.dataSource = self;
    self.colletion.backgroundColor = [UIColor whiteColor];
    [self.colletion registerClass:[AllThemeCollectionViewCell class] forCellWithReuseIdentifier:@"AllThemeCollectionViewCellIdentifier"];
    [self.view addSubview:self.colletion];
}

#pragma mark - CollectionDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AllThemeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AllThemeCollectionViewCellIdentifier" forIndexPath:indexPath];
    if (self.dataSource.count > indexPath.item) {
        cell.model = self.dataSource[indexPath.item];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CXClassProModel *model = self.dataSource[indexPath.item];
    CXProjectClickViewController *projectClickVc = [[CXProjectClickViewController alloc] init];
    [projectClickVc setGetUrlId:model.NewId WithTypeId:@"2" WithTitle:model.title];
    [self.navigationController pushViewController:projectClickVc animated:YES];
}

#pragma mark - CollectionDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.width, self.view.height / 3);
}

#pragma mark - 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return CGFLOAT_MIN;
}


#pragma mark - LeftButtonDidPress
- (void)leftButtonDidPress:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > self.contenty) {
        if (scrollView.contentOffset.y > scrollView.contentSize.height - 700) {
            //加载更多数据
            if (scrolltimes == 0) {
                numOfAll += 20;
                [self getData:kLookMoreGETURL withOffset:[NSString stringWithFormat:@"%d", numOfAll]];
                scrolltimes = 1;
            }
        }
    }
}



#pragma mark - 内存泄露
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
